#include "PlusMinusButton.h"
#include <QEnterEvent>
#include <cmath>

PlusMinusButton::PlusMinusButton(PlusMinuType type, QWidget* parent)
    : QPushButton(parent),
      m_type(type),
      m_buttonShape(ButtonShape::Rounded),  // 默认圆角
      m_borderColor(Qt::black),
      m_borderWidth(2),
      m_symbolColor(Qt::black),
      m_cornerRadius(8),  // 默认圆角半径
      m_isHovered(false)
{
    setFixedSize(80, 80);
    setCursor(Qt::PointingHandCursor);
    setAttribute(Qt::WA_Hover);
    setStyleSheet("background-color: transparent;");
    generateCache();
}

void PlusMinusButton::setType(PlusMinuType type)
{
    if (m_type != type)
    {
        m_type = type;
        generateCache();
        update();
    }
}

void PlusMinusButton::setBorderColor(const QColor& color)
{
    if (m_borderColor != color)
    {
        m_borderColor = color;
        generateCache();
        update();
    }
}

void PlusMinusButton::setBorderWidth(int width)
{
    if (m_borderWidth != width && width > 0)
    {
        m_borderWidth = width;
        generateCache();
        update();
    }
}

void PlusMinusButton::setSymbolColor(const QColor& color)
{
    if (m_symbolColor != color)
    {
        m_symbolColor = color;
        generateCache();
        update();
    }
}

void PlusMinusButton::setButtonShape(ButtonShape shape)
{
    if (m_buttonShape != shape)
    {
        m_buttonShape = shape;
        update();
    }
}

void PlusMinusButton::setCornerRadius(int radius)
{
    if (m_cornerRadius != radius && radius >= 0)
    {
        m_cornerRadius = radius;
        update();
    }
}

void PlusMinusButton::generateCache()
{
    m_cache = QPixmap(size());
    m_cache.fill(Qt::transparent);
    QPainter painter(&m_cache);
    painter.setRenderHint(QPainter::Antialiasing, true);

    int centerX = width() / 2;
    int centerY = height() / 2;

    switch (m_type)
    {
        case PlusMinuType::Plus:
            drawPlusSymbol(painter, centerX, centerY);
            break;
        case PlusMinuType::Minus:
            drawMinusSymbol(painter, centerX, centerY);
            break;
    }
}

void PlusMinusButton::drawPlusSymbol(QPainter& painter, int centerX, int centerY)
{
    int symbolWidth = qMax(2, static_cast<int>(ceil(width() * 0.1)));  // 符号线宽
    int symbolLength = width() * 0.4;                                  // 符号长度

    painter.setPen(QPen(m_symbolColor, symbolWidth, Qt::SolidLine, Qt::RoundCap));
    painter.drawLine(centerX, centerY - symbolLength / 2, centerX, centerY + symbolLength / 2);  // 竖线
    painter.drawLine(centerX - symbolLength / 2, centerY, centerX + symbolLength / 2, centerY);  // 横线
}

void PlusMinusButton::drawMinusSymbol(QPainter& painter, int centerX, int centerY)
{
    int symbolWidth = qMax(2, static_cast<int>(ceil(width() * 0.1)));  // 符号线宽
    int symbolLength = width() * 0.4;                                  // 符号长度

    painter.setPen(QPen(m_symbolColor, symbolWidth, Qt::SolidLine, Qt::RoundCap));
    painter.drawLine(centerX - symbolLength / 2, centerY, centerX + symbolLength / 2, centerY);  // 横线
}

void PlusMinusButton::drawBackground(QPainter& painter, const QRect& rect)
{
    switch (m_buttonShape)
    {
        case ButtonShape::Square:
            // 方形
            painter.drawRect(rect);
            break;

        case ButtonShape::Rounded:
            // 圆角方形
            painter.drawRoundedRect(rect, m_cornerRadius, m_cornerRadius);
            break;

        case ButtonShape::Circle:
            // 圆形
            painter.drawEllipse(rect);
            break;
    }
}

void PlusMinusButton::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    // 背景色
    QColor backgroundColor = Qt::transparent;
    if (isDown())
    {
        backgroundColor = QColor(200, 200, 200, 120);
    }
    else if (m_isHovered)
    {
        backgroundColor = QColor(220, 220, 220, 50);
    }

    // 绘制背景
    QRect innerRect = rect().adjusted(m_borderWidth, m_borderWidth, -m_borderWidth, -m_borderWidth);
    painter.setBrush(backgroundColor);
    painter.setPen(Qt::NoPen);
    drawBackground(painter, innerRect);

    // 绘制边框
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QPen(m_borderColor, m_borderWidth));
    drawBackground(painter, innerRect);

    // 绘制符号
    painter.drawPixmap(0, 0, m_cache);
}

bool PlusMinusButton::event(QEvent* event)
{
    switch (event->type())
    {
        case QEvent::HoverEnter:
            m_isHovered = true;
            update();
            break;
        case QEvent::HoverLeave:
            m_isHovered = false;
            update();
            break;
        default:
            break;
    }
    return QPushButton::event(event);
}

void PlusMinusButton::resizeEvent(QResizeEvent* event)
{
    QPushButton::resizeEvent(event);
    generateCache();
}